using System;
using NCS.Library;
using NCS.Library.Components.Modules;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library.Components.Portal;

namespace NCS.Module.CMS.Library
{
    public class CMSWebControl : ModuleControlBase
    {
        private int zoneTabId;
        private int contentTabId;
        protected ZoneInfo curZone;
        protected DistributionInfo curDistInfo;
        private int portalid;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());
            zoneTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Zone_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            contentTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Content_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);

            int curZoneId = ConvertUtility.ToInt32(Request.QueryString["catid"], 0);

            if (string.IsNullOrEmpty(Request.QueryString["catid"]))
            {
                object zone_active_obj = this.Portal[Definition.Portal_Zone_Active + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()];
                if (curZoneId != null)
                    curZoneId = Int32.TryParse(zone_active_obj.ToString(), out curZoneId) ? curZoneId : 0;
            }

            curZone = ZoneController.GetZone(curZoneId);

            int distId = ConvertUtility.ToInt32(Request.QueryString["distid"]);
            if (distId != 0)
            {
                curDistInfo = DistributionController.GetDistribution(distId);
                if (curZone == null && curDistInfo != null) curZone = ZoneController.GetZone(curDistInfo.Distribution_ZoneID);
            }
        }

        protected string BuildUrlByZone(int zoneId, int realUrl)
        {
            portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);

            if (realUrl == 0)
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + zoneTabId + "&catid=" + zoneId;
            }
            else
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + realUrl + "&catid=" + zoneId;
            }

            //portalid = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);

            //if (realUrl == 0)
            //{
            //    return "/portalid/" + portalid + "/tabid/" + zoneTabId + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
            //else
            //{
            //    return "/portalid/" + portalid + "/tabid/" + realUrl + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            //}
        }
        protected string BuildUrlByGopY(int gopyID)
        {
            //int TabId = ConvertUtility.ToInt32(Portal[Definition.Portal_GopY_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            //return "/portalid/" + portalid + "/tabid/" + TabId + "/gopy/" + gopyID + AppEnv.DEFAULT_EXTENTSION;

            int TabId = ConvertUtility.ToInt32(Portal[Definition.Portal_GopY_Tab + "_" + portalid + "_" + AppEnv.GetLanguage()]);
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + TabId + "&gopy=" + gopyID;
        }

        protected string BuildUrlByZoneSubPortal(int zoneId, int realUrl)
        {
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            if (realUrl == 0)
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + zoneTabId + "&catid=" + zoneId;
            }
            else
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + realUrl + "&catid=" + zoneId;
            }
            
            /*portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            if (realUrl == 0)
            {
                return "/portalid/" + portalid + "/tabid/" + zoneTabId + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            }
            else
            {
                return "/portalid/" + portalid + "/tabid/" + realUrl + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
            }*/
        }

        protected string BuildUrlByZoneSubPortal(string _portalid, string zoneId, string realUrl)
        {
            portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            if (realUrl == "0")
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + zoneTabId + "&catid=" + zoneId;
            }
            else
            {
                return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + realUrl + "&catid=" + zoneId;
            }

            //return "/portalid/" + _portalid + "/tabid/" + realUrl + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlByDistId(int distId, int zoneId)
        {
            return BuildUrlByDistId(distId, zoneId, AppEnv.PortalId());
        }
        protected string BuildUrlByDistId(int distId, int zoneId, string portalid)
        {
            int _portalid = ConvertUtility.ToInt32(portalid);
            int _contentTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Content_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]);

            //return "/portalid/" + _portalid + "/tabid/" + _contentTabId + "/catid/" + zoneId + "/distid/" + distId + AppEnv.DEFAULT_EXTENTSION;
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + _contentTabId + "&catid=" + zoneId + "&distid=" + distId;
        }
        protected string BuildUrlByDistId(int distId, int zoneId, string tabid, string _portalid)
        {
            //return "/portalid/" + _portalid + "/tabid/" + tabid + "/catid/" + zoneId + "/distid/" + distId + AppEnv.DEFAULT_EXTENTSION;
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + tabid + "&catid=" + zoneId + "&distid=" + distId;
        }
        protected string BuildUrlByDistIdFromSearchResult(int distId, int zoneId, int _portalid)
        {

            int _contentTabId = ConvertUtility.ToInt32(Portal[Definition.Portal_Content_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]);

            //return "/portalid/" + _portalid + "/tabid/" + _contentTabId + "/catid/" + zoneId + "/distid/" + distId + AppEnv.DEFAULT_EXTENTSION;
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + _contentTabId + "&catid=" + zoneId + "&distid=" + distId;
        }

        protected string BuildUrlbyPortal()
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]); 
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION; 
        }

        protected string BuildUrlbyPortal(int _portalid)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + _portalid + "&tabid=" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]);
            //return "/portalid/" + _portalid + "/tabid/" + ConvertUtility.ToInt32(Portal[Definition.Portal_Home_Tab + "_" + _portalid + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION;
        }

        
        protected string BuildUrlbyEvent()
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Event_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]); ;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Event_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlbyEvent(int eventid)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_NewsInEvent_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&eventid=" + eventid;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_NewsInEvent_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/eventid/" + eventid + AppEnv.DEFAULT_EXTENTSION;
        }
        protected string BuildUrlSearch(string key)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/keysearch/" + key + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlSearchAdvance(string key, string opt)
        {
            return BuildUrlSearchAdvance(AppEnv.PortalId(), key, opt);
        }
        protected string BuildUrlSearchAdvance(string key, string opt, string portalid)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            //return "/portalid/" + portalid + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/key/" + key + "/opt/" + opt + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlRssAdvance(string key, string opt)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + AppEnv.PortalId() + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&keysearch=" + key;
            //return "/portalid/" + AppEnv.PortalId() + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Rss_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/key/" + key + "/opt/" + opt + AppEnv.DEFAULT_EXTENTSION;
        }

        protected string BuildUrlByZoneForRss(int zoneId, int realUrl)
        {
            return AppEnv.DEFAULT_PAGE + "?portalid=" + portalid + "&tabid=" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Rss_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "&catid=" + zoneId;
            //return "/portalid/" + portalid + "/tabid/" + ConvertUtility.ToInt32(NCS.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Rss_Tab + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]) + "/catid/" + zoneId + AppEnv.DEFAULT_EXTENTSION;
        }
        public string BuildUrlBySendMail()
        {
            return "/SendMail.aspx?url=" + this.Request.Url.AbsoluteUri;
        }

        public string BuildUrlByPrintPreview()
        {
            return "/PrintPreView.aspx?distid=" + this.curDistInfo.Distribution_ID;
        }

        public string BuildUrlByPdf()
        {
            return "/PdfView.aspx?distid=" + this.curDistInfo.Distribution_ID;
        }
    }
}
